//
//  QYHoldingCateListViewController.m
//  QingYue
//
//  Created by Zheng on 2017/7/24.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYHoldingCateListViewController.h"
#import "QYHoldingListBookViewController.h"

@interface QYHoldingCateListViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, copy) NSArray *dataArr;


@end

@implementation QYHoldingCateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"分类";
    
    
    _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"catecell"];
    [self.view addSubview:_myTableView];

    
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];

}

- (void)loadData {
    
    [[YCRequestDataService sharedManager] Post:@"GetHoldingType" parameters:nil success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            NSArray *holdingList = response[@"holdingList"];
            _dataArr = [holdingList copy];
            dispatch_async(dispatch_get_main_queue(), ^{
                [_myTableView reloadData];
            });
        }
        
    } failure:^{
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"catecell" forIndexPath:indexPath];
    
    if (_dataArr.count != 0) {
        cell.textLabel.text = _dataArr[indexPath.row][@"name"];
        cell.textLabel.font = Font(14);
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QYHoldingListBookViewController *hlbVC = [[QYHoldingListBookViewController alloc] init];
    hlbVC.holdingType = _dataArr[indexPath.row][@"type"];
    hlbVC.hidesBottomBarWhenPushed = YES;
    hlbVC.title = _dataArr[indexPath.row][@"name"];
    [self.navigationController pushViewController:hlbVC animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
